package com.ts.system.impl;

import com.ts.jdbc.SysDB;
import com.ts.system.dao.SystemDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LYK on 2017/4/10.
 */
@Repository
public class SystemDaoImpl implements SystemDao{
	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;

	//获取department的list
	public List<Map<String, Object>> getDepartmentList() throws Exception {
		StringBuilder sql=new StringBuilder();
		sql.append("select rownum as NO,v.* from ( ");
		sql.append(" select d.FCODE,d.FNAME,");
		sql.append("case d.fdeptlevel");
		sql.append(" when 0 then '税务部门'");
		sql.append(" when 1 then '总局'");
		sql.append(" when 2 then '省局'");
		sql.append(" when 3 then '地市局'");
		sql.append(" when 4 then '区县局'");
		sql.append(" else '未知' end as FDEPTLEVEL,");
		sql.append("case d.fisstop");
		sql.append("  when 1 then '是'");
		sql.append("  when 0 then '否'");
		sql.append("  else '未知' end as FISSTOP,");
		sql.append("p.fname as FPARENT ");
		sql.append("from S_DEPARTMENT d ");
		sql.append("left join S_DEPARTMENT p ");
		sql.append("on d.fparent = p.fcode ");
		sql.append(" order by 1 ");
		sql.append(" ) v ");
		return SysDB.getRows(jdbcTemplate,sql.toString(),new HashMap<String, Object>());
	}

	//获取所属机构的列表
	public String getParentDepartmentList() throws Exception {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from S_DEPARTMENT order by FCODE");
		String str;
		HashMap map;
		List<String> resultList = new ArrayList<String>();
		List rowList = SysDB.getRows(jdbcTemplate,sql.toString(),new HashMap<String, Object>());
		StringBuilder jsonStr = new StringBuilder();
		jsonStr.append("[");
		for(Object resultMap:rowList){
			map = (HashMap)resultMap;
			str = "{FCODE:'"+(String)map.get("FCODE")+"',FPARENT:'"+(String)map.get("FPARENT")+"',FNAME:'"+(String)map.get("FNAME")+"',FDEPTLEVEL:'"+map.get("FDEPTLEVEL").toString()+"'},";
			jsonStr.append(str);
		}
		jsonStr.append("]");
		return jsonStr.toString();
	}

	//删除机构
	public Boolean deleteDepartment(String fcode) throws Exception {
		StringBuilder findsql=new StringBuilder();
		findsql.append("select count(*) as count from S_DEPARTMENT where FPARENT = '");
		findsql.append(fcode);
		findsql.append("'");
		List rowList = SysDB.getRows(jdbcTemplate,findsql.toString(),new HashMap<String, Object>());
		Map countMap =  (Map) rowList.get(0);
		BigDecimal count = (BigDecimal) countMap.get("COUNT");
		int countInt = count.intValue();
		if(countInt > 0 )
			return false;

		StringBuilder deleteSql=new StringBuilder();
		deleteSql.append("delete from S_DEPARTMENT where FCODE = '");
		deleteSql.append(fcode);
		deleteSql.append("'");
		System.out.println(deleteSql);
		int result = SysDB.update(jdbcTemplate,deleteSql.toString(),new HashMap<String, Object>());
		if(result>0)
			return true;
		else
			return false;
	}
}
